<?php defined('SYSPATH') or die('No direct script access.');

// Книги
class Model_Books
{  
    public function get_global_popular_books($count = 5)
    {
        $query = DB::select('book_id', 'name', 'author', 'year', 'picture', 'price')
                ->from('popular_books')
                ->order_by(DB::expr('RAND()'))
                ->limit($count);
                
        $books  = $query->execute()->as_array();
        
        
        
        return $books;
    }
 
    public function get_popular_books($category_ids, $count)
    {
        $query = DB::select('book_id', 'name', 'author', 'year', 'picture', 'price')
                ->from('books')
                ->where('category_id', 'IN', $category_ids)
                ->and_where('rating', '>', 400)
                ->order_by('rating', 'DESC')
                ->limit($count);
                
        /*echo $query->__toString();
        die();  */
                
        $books  = $query->execute()->as_array();
        
        return $books;   
    }
    
    public function get_related_books($book_id, $count = 8)
    {
        $book = $this->get_book($book_id);
        
        $query = DB::select('book_id', 'name', 'author', 'year', 'picture', 'price')
                    ->from('books')
                    ->order_by(DB::expr('RAND()'))
                    ->where('category_id', '=', $book['category_id'])
                    ->and_where('book_id', '!=', $book_id)
                    ->limit($count);
                    
        $books = $query->execute()->as_array();

        return $books; 
    }
    
    public function get_books($category_id, $count, $offset, $order_by = null)
    {
        if(!$order_by)
            $order_by = array('rating', 'DESC');  
        
        $query = DB::select('book_id', 'name', 'author', 'year', 'description', 'picture', 'price')
                    ->from('books')
                    ->where('category_id', '=', $category_id)
                    ->order_by($order_by[0], $order_by[1])
                    ->limit($count)->offset($offset);
                   
         $books = $query->execute()->as_array();
         
         return $books;  
    }
    
    public function get_count_books($category_id)
    {
        $query = DB::select(array('COUNT("book_id")', 'count'))
                    ->from('books')
                    ->where('category_id', '=', $category_id);
                
        $result = $query->execute()->as_array();

        return $result[0]['count'];
    }
    
    public function get_book($book_id)
    {
        $query = DB::select()
                    ->from('books')
                    ->where('book_id', '=', $book_id);
                
        $books = $query->execute()->as_array();

        return $books[0]; 
    } 
    
    public function increase_rating($rating, $book_id)
    {
        $query = DB::update('books')
                        ->set(array('rating' => DB::expr('rating + '.$rating)))
                        ->where('book_id', '=', $book_id);
                               
        $result = $query->execute();
        
        return $result;     
    }   
    
    public function search($query)
    {
        if(!$query) {
            return null;
        }
        
        if(!mb_strlen($query)) {
            return null;
        }
        
        $sql = DB::query(Database::SELECT, "SELECT * FROM books
                                            WHERE MATCH (name, author) AGAINST (:query)
                                            LIMIT 20");
        $books = $sql->param(':query', $query)->execute()->as_array();
        return $books;
    }    
} // End Books